package com.leoman.role.service.impl;

import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.role.dao.ModuleDao;
import com.leoman.role.dao.RoleDao;
import com.leoman.role.dao.RoleModuleDao;
import com.leoman.role.entity.Role;
import com.leoman.role.entity.RoleModule;
import com.leoman.role.service.RoleService;
import com.leoman.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 蔡琦 on 2016/7/18 0018.
 *
 */
@Service
public class RoleServiceImpl extends GenericManagerImpl<Role, RoleDao> implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RoleModuleDao roleModuleDao;

    @Autowired
    private ModuleDao moduleDao;

    @Override
    public Page<Role> page(Integer pageNum, Integer pageSize) {
        PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id");

        Page<Role> page = roleDao.findAll(new Specification<Role>() {
            @Override
            public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<>();

                /*Predicate pre2 = cb.notEqual(root.get("id").as(Long.class), 6);
                predicateList.add(pre2);*/
                Predicate pre1 = cb.notEqual(root.get("status").as(Integer.class), 1);
                predicateList.add(pre1);

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, pageRequest);

        return page;
    }

    @Override
    public Role getById(Long roleId) {
        return roleDao.findOne(roleId);
    }

    @Override
    @Transactional
    public void saveAll(HttpServletRequest request, Long id, String name, String types) {
        // 解析权限id
        Long[] moduleIds = JsonUtil.json2Obj(types, Long[].class);

        Role roles;
        RoleModule rolemodules;

        if (null == id) {
            roles = new Role();
            roles.setCreateDate(System.currentTimeMillis());
            roles.setUpdateDate(System.currentTimeMillis());
            roles.setStatus(0);
            roles.setName(name);

            roleDao.save(roles);
        } else {
            roles = roleDao.findOne(id);
            roles.setName(name);
            roles.setUpdateDate(System.currentTimeMillis());
            roleDao.save(roles);
        }

        // 先删除该角色对应的所有权限
        List<RoleModule> list = roleModuleDao.findListByRoleId(roles.getId());
        for (RoleModule rolemodules1 : list) {
            roleModuleDao.delete(rolemodules1);
        }

        // 然后逐个保存该角色的权限
        for (Long moduleId : moduleIds) {
            rolemodules = new RoleModule();
            rolemodules.setRole(roles);
            rolemodules.setModule(moduleDao.findOne(moduleId));
            rolemodules.setCreateDate(System.currentTimeMillis());

            roleModuleDao.save(rolemodules);
        }
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findList();
    }
}
